﻿using Corina.Datamodel.Entities;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows.Controls;
using System.Windows.Input;
using System.Linq;
using Corina.Lantern.DataService;
using Corina.Commands;
using System.Windows;

namespace Corina.Lantern.Hub.Viewmodels
{
    public class MainViewmodel : DataContainer
    {
        public MainViewmodel() {
            AccountChanged += CurrentAccountChanged;
            CoreClient.GetTopurlsCompleted += GetFetchedTopurlCompleted;
            CoreClient.GetFetchedUrlCompleted += GetFetchedUrlCompleted;
            App.Current.Host.Content.FullScreenChanged += FullScreenChanged;
        }

        private void FullScreenChanged(object sender, EventArgs e)
        {
            FullScreen = App.Current.Host.Content.IsFullScreen;
        }

        #region Methods
        private void GetFetchedUrlCompleted(object sender, GetFetchedUrlCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                WeburlCollection = new ObservableCollection<WeburlInfo>(e.Result.Entities.OfType<WeburlInfo>());
                SelectedTopurl = TopurlCollection.FirstOrDefault();
                SelectedWeburl = Navigations.FirstOrDefault();
            }
        }

        private void GetFetchedTopurlCompleted(object sender, GetTopurlsCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                TopurlCollection = new ObservableCollection<WeburlTopInfo>(e.Result.Entities.OfType<WeburlTopInfo>());
                CoreClient.GetFetchedUrlAsync(CurrentAccount.Account, CurrentAccount.Password);
            }
        }

        private void CurrentAccountChanged(object sender, EventArgs e)
        {
            if (CurrentAccount != null)
            {
                CoreClient.GetTopurlsAsync(CurrentAccount.Account, CurrentAccount.Password);
                ShowHeader = false;
            }
        }
        #endregion

        #region Navigations
        public IEnumerable<WeburlInfo> Navigations
        {
            get
            {
                if (SelectedTopurl == null)
                {
                    return null;
                }
                return WeburlCollection == null ? null : WeburlCollection.Where(t => t.TopurlId == SelectedTopurl.Id);
            }
        }

        #region Urls
        private ObservableCollection<WeburlTopInfo> _topurlCollection;
        public ObservableCollection<WeburlTopInfo> TopurlCollection
        {
            get { return _topurlCollection; }
            set
            {
                _topurlCollection = value;
                RaisePropertyChanged(() => TopurlCollection);
            }
        }

        private WeburlTopInfo _selectedTopurl;
        public WeburlTopInfo SelectedTopurl
        {
            get { return _selectedTopurl; }
            set
            {
                _selectedTopurl = value;
                RaisePropertyChanged(() => SelectedTopurl);
                RaisePropertyChanged(() => Navigations);
            }
        }

        private bool _showAllUrls = false;
        public bool ShowAllUrls
        {
            get { return _showAllUrls; }
            set
            {
                _showAllUrls = value;
                RaisePropertyChanged(() => ShowAllUrls);
            }
        }

        private ObservableCollection<WeburlInfo> _weburlCollection;
        public ObservableCollection<WeburlInfo> WeburlCollection
        {
            get { return _weburlCollection; }
            set
            {
                _weburlCollection = value;
                RaisePropertyChanged(() => WeburlCollection);
            }
        }

        private WeburlInfo _selectedWeburl;
        public WeburlInfo SelectedWeburl
        {
            get { return _selectedWeburl; }
            set
            {
                if (value != null && _selectedWeburl != value)
                {
                    if (SelectedTopurl.Id != value.TopurlId)
                    {
                        SelectedTopurl = TopurlCollection.FirstOrDefault(t => t.Id == value.TopurlId);
                    }
                }
                _selectedWeburl = value;
                RaisePropertyChanged(() => SelectedWeburl);
                if (SelectedWeburl != null)
                {
                    ViewUrl = SelectedWeburl.Url;
                }
                ShowAllUrls = false;
            }
        }
        #endregion

        public override void GotoView(string code)
        {
            if (WeburlCollection.Count(t => t.PageCode == code) > 0)
            {
                SelectedWeburl = WeburlCollection.FirstOrDefault(t => t.PageCode == code);
            }
        }

        #endregion

        #region Page Control
        public ContentControl ElementView
        {
            get { return Helpers.XamlHelper.LoadXamlFromUri<ContentControl>("/Corina.Lantern.Hub;component/HomeUI/CocoaUI.xaml"); }
        }

        private bool _fullScreen = false;
        public bool FullScreen
        {
            get { return _fullScreen; }
            set
            {
                _fullScreen = value;
                RaisePropertyChanged(() => FullScreen);
                App.Current.Host.Content.IsFullScreen = FullScreen;
                RaisePropertyChanged(() => VerticalScrollBarVisibility);
            }
        }

        private bool _showHeader = true;
        public bool ShowHeader
        {
            get { return _showHeader; }
            set
            {
                _showHeader = value;
                RaisePropertyChanged(() => ShowHeader);
            }
        }
        #endregion

        #region Command
        public ICommand LogoutCommand
        {
            get
            {
                return new GenericCommand()
                {
                    ExecuteCallback = delegate
                    {
                        TopurlCollection = null;
                        WeburlCollection = null;
                        SelectedTopurl = null;
                        SelectedWeburl = null;
                        CurrentAccount = null;
                        ViewUrl = "/Login";
                    }
                };
            }
        }
        #endregion

        #region ScrollViewer
        public ScrollBarVisibility VerticalScrollBarVisibility
        {
            get
            {
                if(FullScreen)
                {
                    return ScrollBarVisibility.Hidden;
                }
                return Environment.OSVersion.Platform == PlatformID.MacOSX ? ScrollBarVisibility.Auto : ScrollBarVisibility.Hidden;
            }
        }

        public void ContentGridSizeChanged(object sender, SizeChangedEventArgs e)
        {
            //ScrollViewer viewer = (ScrollViewer)((sender as Grid).Parent);
            //if (!e.NewSize.IsEmpty && e.NewSize.Height > viewer.ActualHeight)
            //{
            //    ShowHeader = false;
            //}
            //else
            //{
            //    ShowHeader = true;
            //}
        }

        #endregion
    }
}
